package org.test.jiazi_1.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.test.jiazi_1.service.ImportOrExportService;

import java.io.IOException;

/**
 * @Author: 2251300222 唐文涛
 * @Date: 2025/10/23/19:58
 * @Description:
 */

@Slf4j
@RestController
@RequestMapping("classBase")
@Tag(name = "导入导出控制层")
public class ImportOrExportController {

    @Resource
    ImportOrExportService importOrExportService;
    @Operation(summary = "导入")
    @PostMapping("/import")
    public void importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        long stime = System.currentTimeMillis();
        // Stream the uploaded file directly to service to avoid temp files
        try (java.io.InputStream in = file.getInputStream()) {
            importOrExportService.importExcel(in);
        }
        long etime = System.currentTimeMillis();
        System.out.printf("执行时长：%d 毫秒.", (etime - stime));
    }


    @Operation(summary = "根据学期导出")
    @PostMapping("/export/{semester}")
    public void exportExcel(@PathVariable String semester, HttpServletResponse response) {
        importOrExportService.exportExcel(semester, response);
    }



}
